package com.ruoyi.mateTemplate.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateTemplate.domain.MpTemplate;
import com.ruoyi.mateTemplate.service.MpTemplateService;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.material.service.MateProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateProductTemplate")
public class MpTemplateController extends BaseController {

    @Autowired
    MpTemplateService mpTemplateService;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateProductService mateProductService;

    @PostMapping("/addTemplate")
    public AjaxResult addTemplate(@RequestBody MpTemplate mpTemplate) {
        int num = mpTemplateService.addTemplate(mpTemplate);
        if (num == 0) {
            return AjaxResult.error("填充数据失败");
        } else {
            return AjaxResult.success("填充数据成功");
        }
    }

    @GetMapping("/selectMatemanufcatuter")
    public List<MaterialManufactuter> selectManufacturer() {
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        List<MaterialManufactuter> list = mateManufactuterService.selectMateManufactuter(materialManufactuter);
        return list;
    }

    @GetMapping("/selectTableRow")
    public JSONObject tableSelect(@RequestParam(value = "mateProductType", required = false) String mateProductType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        Map map = new HashMap<String, Object>();
        map.put("mateProductType", mateProductType);
        map.put("companyId", SecurityUtils.getLoginUser().getCompanyId());
        MaterialProduct materialProduct = new MaterialProduct();
        materialProduct.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        List<MaterialProduct> products = mateProductService.selectMateriProduct(materialProduct);
        JSONArray array = new JSONArray();
        for (MaterialProduct product : products) {
            JSONObject obj = new JSONObject();
            obj.put("mateProductName", product.getMateProductName());
            obj.put("mateproductId", product.getMateproductId());
            array.add(obj);
        }
        jsonObject.put("data", array);
        return jsonObject;
    }

    @GetMapping("/selectLastRecord")
    public Map<String, Object> selectLastRecord() {
        Map<String, Object> resMap = mpTemplateService.selectLastRecord(SecurityUtils.getDeptId());
        return resMap;
    }

}
